﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using DataStore;
using System.Configuration;
using System.Web.Services;

namespace Web
{
    public partial class Sites : BasePage
    {
        
        [WebMethod]
        public static AjaxResponse Delete(int siteId)
        {
            if (Visitor.Current.IsAnonymous)
                return AjaxResponse.DenyAnonymous();

            var store = GetDataStore();
            store.DeleteSite( siteId );

            return AjaxResponse.Ok();
        }

        [WebMethod]
        public static AjaxResponse ListSites()
        {
            return AjaxResponse.Ok( ListSitesHtml() );
        }
        
        protected void Page_Load( object sender, EventArgs e )
        {            
            UpdateDisplay();
        }


        private void UpdateDisplay()
        {
            litSites.Text = ListSitesHtml();
        }

        private static string ListSitesHtml()
        {
            var store = GetDataStore();

            StringBuilder html = new StringBuilder();
            html.AppendFormat( "<table cellspacing=0><thead><td>Site</td><td>Logfile Path</td><td>Action</td></thead>" );
            foreach (var site in store.Sites)
                html.AppendFormat( "<tr><td><a href=\"files.aspx?siteid={0}\">{1}</a></td><td>{2}</td><td><a href=\"#\" onclick=\"LogView.Admin.DeleteSite({0})\" />Del</a></tr>", site.Id, site.Name, site.Folder );
            html.AppendFormat( "</table>" );

            return html.ToString();            
        }

        protected void btnSave_click(object sender, EventArgs e)
        {
            var store = new Store( ConfigurationManager.AppSettings["dbfile"] );

            store.FindOrCreateSite( txtName.Text, txtFolder.Text );
            UpdateDisplay();
        }
    }
}